package com.cqjtu.controller;

import com.cqjtu.enums.ResultCodeEnum;
import com.cqjtu.pojo.PageBean;
import com.cqjtu.pojo.Result;
import com.cqjtu.pojo.Unit;
import com.cqjtu.service.UnitService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Slf4j
@RestController
public class UnitController {

    @Autowired
    private UnitService unitService;

    //单位查询
    @GetMapping("/units")
    public Result page(@RequestParam(defaultValue = "1") Integer page,
                       @RequestParam(defaultValue = "10") Integer pageSize,
                       String unitName, String unitCategory){

        log.info("分页查询参数：{}，{}",page,pageSize);


        PageBean pageBean = unitService.page(page,pageSize,unitName,unitCategory);

        return Result.success(pageBean);
    }

    //新增单位
    @PostMapping("/units/insert")
    public Result add(@RequestBody Unit unit){
        log.info("添加单位 ：{}", unit);

        unitService.add(unit);

        return Result.success();
    }

    //根据id查询
    @GetMapping("/units/{unitId}")
    public Result getById(@PathVariable Integer unitId){
        log.info("根据id查询单位：{}", unitId);

        Unit unit = unitService.getById(unitId);

        return Result.success(unit);
    }

    //修改单位
    @PutMapping("/units/update")
    public Result update(@RequestBody Unit unit){
        log.info("修改单位：", unit);


        unitService.update(unit);

        return Result.success();
    }

    //根据id删除
    @DeleteMapping("units/{unitIds}")
    public Result deleteById(@PathVariable List<Integer> unitIds){
        log.info("根据id删除单位：{}", unitIds);

        boolean flag = unitService.delete(unitIds);
        if(!flag) return Result.error(ResultCodeEnum.UNIT_NOT_DELETE);
        return Result.success();
    }




}
